package com.sophialex.health.record.views;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.ui.part.ViewPart;

import com.sophialex.health.common.platform.SuperContext;
import com.sophialex.health.record.Activator;
import com.sophialex.health.record.Messages;
import com.sophialex.health.record.common.SOAPServiceDispath;
import com.sophialex.health.record.common.utils.CommonUtils;
import com.sophialex.health.record.common.utils.ImageUtil;
import com.sophialex.health.system.frame.dialog.DialogManager;
import com.sophialex.health.system.frame.resource.RcpResourceManager;
import com.sophialex.health.system.frame.util.ViewUitl;

/**
 * 所有可做的服务列表
 * <p>
 * Title:Health
 * </p>
 * <p>
 * ServiceToDoView
 * </p>
 * Alex Apr 22, 2011 1:17:47 PM create
 * 
 * @author <a href='wanghao924@126.com'>Alex</a> QQ: 873987060;
 * @version 1.0
 */
public class ServiceToDoView extends ViewPart {

	public static final String ID = "com.sophialex.health.record.views.ServiceToDoView"; //$NON-NLS-1$

	protected Composite barContainer;

	protected ExpandBar expandBar;


	protected int detailHeight = 50;

	protected final Image defaultImage1 = RcpResourceManager.getPluginImage(Activator.getDefault(), "icons/view/onlyPatient.png");

	protected final Image item = RcpResourceManager.getPluginImage(Activator.getDefault(), "icons/view/item.bmp");
    
	protected Label imageLabel;
	
	protected Link  serviceLink;
	
	
	public ServiceToDoView() {
	}

	/**
	 * Create contents of the view part.
	 * 
	 * @param parent
	 */
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new FillLayout(SWT.HORIZONTAL));

		barContainer = new Composite(container, SWT.NONE);
		barContainer.setLayout(new FillLayout());
		barContainer.setBackgroundMode(SWT.INHERIT_DEFAULT);
		ExpandBar expandBar = new ExpandBar(barContainer, SWT.V_SCROLL);
		expandBar.setBackground(ImageUtil.COLOR_LIST_BACKGROUND);
		expandBar.setForeground(ImageUtil.BALCK);
		expandBar.setFont(ImageUtil.font_10);
		expandBar.setSpacing(2);
		
		
	    Map<Long, List<Long>>  serviceMap = CommonUtils.getAllService(null);
		
	    if (serviceMap != null && !serviceMap.isEmpty()) {
			int i = 0;
			int k = 0;
			for (Long key : serviceMap.keySet()) {
				List<Long> supList = serviceMap.get(key);
				String title = CommonUtils.getBusTypeName(key);
				Composite comp1 = new Composite(expandBar, SWT.NONE);
				comp1.setLayout(new GridLayout(2, false));
				comp1.setBackground(RcpResourceManager.getColor(255, 255, 255));
				if (supList != null && supList.size() > 0) {
					for (int j = 0; j < supList.size(); j++) {
						final Long sub2 = supList.get(j);
						String detail = CommonUtils.getBusTypeName(sub2);
						imageLabel = new Label(comp1, SWT.NONE);
						imageLabel.setImage(item);
						serviceLink = new Link(comp1, SWT.NONE);
						serviceLink.setText("<a>"+detail+"</a>");
						serviceLink.setToolTipText(detail);
						serviceLink.setBackground(ImageUtil.COLOR_LIST_BACKGROUND);
						serviceLink.addSelectionListener(new SelectionAdapter() {
							public void widgetSelected(SelectionEvent e) {
								if(SuperContext.currentPerson == null){
									DialogManager.invokeWarnDlg(Messages.getString("Common.12"));
									return;
								}
								String viewId = SOAPServiceDispath.serviceMap.get(sub2);
								if(StringUtils.isNotBlank(viewId)){
									BaseView view = (BaseView) ViewUitl.showView(viewId);
									view.clearView();
								}
							}
						});
					} 
				}else{
					supList = new ArrayList();
				}
				ExpandItem item1 = new ExpandItem(expandBar, SWT.NONE);
				item1.setText(title);
				item1.setHeight(supList.size()>0?supList.size()*25:25);
				item1.setControl(comp1);
				item1.setImage(RcpResourceManager.getPluginImage(Activator.getDefault(), "icons/view/"+key+".png"));
				if(i==0){
					item1.setExpanded(true);
				}
                i++;
			}
		}
		initializeToolBar();
		initializeMenu();
	}

	/**
	 * Initialize the toolbar.
	 */
	private void initializeToolBar() {
		IToolBarManager toolbarManager = getViewSite().getActionBars().getToolBarManager();
	}

	/**
	 * Initialize the menu.
	 */
	private void initializeMenu() {
		IMenuManager menuManager = getViewSite().getActionBars().getMenuManager();
	}

	@Override
	public void setFocus() {
		// Set the focus
	}

}
